package com.itlong.cloud.controller.project;

import com.itlong.cloud.POJO.DTO.operate.OperateDeveloperAuthDeviceAuthDTO;
import com.itlong.cloud.POJO.DTO.operate.OperateDeviceAuthCodeGetDevicePageDTO;
import com.itlong.cloud.POJO.DTO.operate.OperateDeviceAuthCodeGetPageDTO;
import com.itlong.cloud.POJO.VO.MessageVO;
import com.itlong.cloud.POJO.VO.operate.OperateDeviceAuthCodeGetDevicePageVO;
import com.itlong.cloud.POJO.VO.operate.OperateDeviceAuthCodeGetPageVO;
import com.itlong.cloud.abstracts.BaseController;
import com.itlong.cloud.annotation.AuthAccessRequired;
import com.itlong.cloud.annotation.LoggerInfo;
import com.itlong.cloud.constants.LogDescConstant;
import com.itlong.cloud.enumerate.ecode.BaseErrorCodeEnum;
import com.itlong.cloud.enumerate.ecode.OperateErrorCodeEnum;
import com.itlong.cloud.project.operate.IOperateDeviceAuthCodeServiceClient;
import com.itlong.cloud.thrown.DataAccessException;
import com.itlong.cloud.utils.network.http.HttpProtocolUtil;
import com.itlong.cloud.utils.page.Page;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * <desc>
 *      设备授权码管理控制器
 * </desc>
 *
 * @createDate 2019/01/16
 */
@RestController
@RequestMapping("/device/authCode")
public class OperateDeviceAuthCodeController extends BaseController {

    @Autowired
    private IOperateDeviceAuthCodeServiceClient iOperateDeviceAuthCodeServiceClient;

    /**
     * <desc>
     *      分页获取设备授权码
     * </desc>
     *
     * @param operateDeviceAuthCodeGetPageDTO 获取设备授权码列表DTO
     * @return 标准格式化响应结果
     * @author wangzhi
     * @createDate 2019/01/16
     */
    @ResponseBody
    @AuthAccessRequired
    @PostMapping("getPage")
    public Object getDeviceAuthCodeByPage(OperateDeviceAuthCodeGetPageDTO operateDeviceAuthCodeGetPageDTO){
        try {
            HttpServletRequest request=((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            operateDeviceAuthCodeGetPageDTO= HttpProtocolUtil.parseRequestParamToDTO(OperateDeviceAuthCodeGetPageDTO.class,request);
            if (operateDeviceAuthCodeGetPageDTO == null) {
                return new MessageVO(BaseErrorCodeEnum.NOT_PARAM.getErrorCode());
            }
            Page<OperateDeviceAuthCodeGetPageVO> operateDeviceAuthCodeGetPageVOS = iOperateDeviceAuthCodeServiceClient.getPage(operateDeviceAuthCodeGetPageDTO);
            return new MessageVO(BaseErrorCodeEnum.SUCCESS.getErrorCode(),operateDeviceAuthCodeGetPageVOS);
        } catch (Exception e) {
            throw new DataAccessException("【运营-项目管理】分页获取设备授权码失败",e);
        }
    }

    /**
     * <desc>
     *      新增设备授权码
     * </desc>
     *
     * @param baiduAuthCode 百度识别授权码，多个用英文逗号相连
     * @return 标准格式化响应结果
     * @author wangzhi
     * @createDate 2019/01/16
     */
    @AuthAccessRequired
    @LoggerInfo(operateDesc = LogDescConstant.OPERATE_DEVICE_AUTH_CODE_SAVE_LOG_DESC)
    @ResponseBody
    @PostMapping("save")
    public Object saveDeviceAuthCode(String baiduAuthCode){
        try {
            HttpServletRequest request=((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            baiduAuthCode= request.getAttribute("baiduAuthCode")==null?null:request.getAttribute("baiduAuthCode").toString();
            if (StringUtils.isBlank(baiduAuthCode)) {
                return new MessageVO(BaseErrorCodeEnum.NOT_PARAM.getErrorCode());
            }
            iOperateDeviceAuthCodeServiceClient.saveDeviceAuthCode(baiduAuthCode);
            return new MessageVO(BaseErrorCodeEnum.SUCCESS.getErrorCode());
        } catch (Exception e) {
            throw new DataAccessException("【运营-项目管理】新增设备授权码失败",e);
        }
    }

    /**
     * <desc>
     *      查询单个设备授权码
     * </desc>
     *
     * @param operateDeviceAuthCodeGetPageDTO 获取设备授权码列表DTO
     * @return 标准格式化响应结果
     * @author wangzhi
     * @createDate 2019/01/21
     */
    @ResponseBody
    @PostMapping("getOne")
    public Object getOne(OperateDeviceAuthCodeGetPageDTO operateDeviceAuthCodeGetPageDTO){
        try {
            HttpServletRequest request=((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            operateDeviceAuthCodeGetPageDTO= HttpProtocolUtil.parseRequestParamToDTO(OperateDeviceAuthCodeGetPageDTO.class,request);
            if (operateDeviceAuthCodeGetPageDTO == null || StringUtils.isBlank(operateDeviceAuthCodeGetPageDTO.getBaiduAuthCode())) {
                return new MessageVO(BaseErrorCodeEnum.NOT_PARAM.getErrorCode());
            }
            OperateDeviceAuthCodeGetPageVO operateDeviceAuthCodeGetPageVO = iOperateDeviceAuthCodeServiceClient.getOne(operateDeviceAuthCodeGetPageDTO);
            return new MessageVO(BaseErrorCodeEnum.SUCCESS.getErrorCode(),operateDeviceAuthCodeGetPageVO);
        } catch (Exception e) {
            throw new DataAccessException("【运营-项目管理】查询单个设备授权码失败",e);
        }
    }

    /**
     * <desc>
     *      获取设备授权码相关设备列表
     * </desc>
     *
     * @param operateDeviceAuthCodeGetDevicePageDTO 获取设备授权码相关设备列表DTO
     * @return 标准格式化响应结果
     * @author wangzhi
     * @createDate 2019/03/12
     */
    @ResponseBody
    @PostMapping("getDevicePage")
    public Object getDevicePage(OperateDeviceAuthCodeGetDevicePageDTO operateDeviceAuthCodeGetDevicePageDTO){
        try {
            HttpServletRequest request=((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            operateDeviceAuthCodeGetDevicePageDTO= HttpProtocolUtil.parseRequestParamToDTO(OperateDeviceAuthCodeGetDevicePageDTO.class,request);
            if (operateDeviceAuthCodeGetDevicePageDTO == null) {
                return new MessageVO(BaseErrorCodeEnum.NOT_PARAM.getErrorCode());
            }
            if (StringUtils.isBlank(operateDeviceAuthCodeGetDevicePageDTO.getProjectId())) {
                return new MessageVO(OperateErrorCodeEnum.PUB_PROJECT_ID_EMPTY_ERR.getErrorCode());
            }
            Page<OperateDeviceAuthCodeGetDevicePageVO> page = iOperateDeviceAuthCodeServiceClient.getDevicePage(operateDeviceAuthCodeGetDevicePageDTO);
            return new MessageVO(BaseErrorCodeEnum.SUCCESS.getErrorCode(),page);
        } catch (Exception e) {
            throw new DataAccessException("【运营-项目管理】获取设备授权码相关设备列表失败",e);
        }
    }

    /**
     * <desc>
     *      购买（设备绑定设备授权码）
     * </desc>
     *
     * @param projectId 项目编号
     * @param deviceUnique 设备序列号，多个逗号相连
     * @return 标准格式化响应结果
     * @author wangzhi
     * @createDate 2019/03/12
     */
    @AuthAccessRequired
    @LoggerInfo(operateDesc = LogDescConstant.OPERATE_DEVICE_AUTH_CODE_BUY_LOG_DESC)
    @ResponseBody
    @PostMapping("buy")
    public Object buy(String projectId, String deviceUnique){
        try {
            HttpServletRequest request=((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            projectId= request.getAttribute("projectId")==null?null:request.getAttribute("projectId").toString();
            deviceUnique= request.getAttribute("deviceUnique")==null?null:request.getAttribute("deviceUnique").toString();
            if (StringUtils.isBlank(projectId)) {
                return new MessageVO(OperateErrorCodeEnum.PUB_PROJECT_ID_EMPTY_ERR.getErrorCode());
            }
            if (StringUtils.isBlank(deviceUnique)) {
                return new MessageVO(BaseErrorCodeEnum.NOT_PARAM.getErrorCode());
            }
            Integer res = iOperateDeviceAuthCodeServiceClient.buy(projectId, deviceUnique);
            if (res != null && res == 0) {
                return new MessageVO(OperateErrorCodeEnum.NO_ENOUGH_AUTH_CODE.getErrorCode());
            }
            return new MessageVO(deviceUnique,BaseErrorCodeEnum.SUCCESS.getErrorCode());
        } catch (Exception e) {
            throw new DataAccessException("【运营-项目管理】购买（设备绑定设备授权码）失败",e);
        }
    }

    /**
     * <desc>
     *      解绑（设备与设备授权码解除绑定）
     * </desc>
     *
     * @param operateDeviceAuthCodeGetPageDTO 获取设备授权码列表DTO
     * @return 标准格式化响应结果
     * @author wangzhi
     * @createDate 2019/03/12
     */
    @AuthAccessRequired
    @LoggerInfo(operateDesc = LogDescConstant.OPERATE_DEVICE_AUTH_CODE_UNBIND_LOG_DESC)
    @ResponseBody
    @PostMapping("unbind")
    public Object unbind(OperateDeviceAuthCodeGetPageDTO operateDeviceAuthCodeGetPageDTO){
        try {
            HttpServletRequest request=((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            operateDeviceAuthCodeGetPageDTO=HttpProtocolUtil.parseRequestParamToDTO(OperateDeviceAuthCodeGetPageDTO.class,request);
            if (operateDeviceAuthCodeGetPageDTO == null || StringUtils.isBlank(operateDeviceAuthCodeGetPageDTO.getCodeId())) {
                return new MessageVO(BaseErrorCodeEnum.NOT_PARAM.getErrorCode());
            }
            int res = iOperateDeviceAuthCodeServiceClient.unbind(operateDeviceAuthCodeGetPageDTO);
            if (res == 0) {
                return new MessageVO(OperateErrorCodeEnum.NOT_PARAM.getErrorCode());
            }
            return new MessageVO(operateDeviceAuthCodeGetPageDTO.getCodeId(),BaseErrorCodeEnum.SUCCESS.getErrorCode());
        } catch (Exception e) {
            throw new DataAccessException("【运营-项目管理】解绑（设备与设备授权码解除绑定）失败",e);
        }
    }

}
